All files / web/src/app/api/classrooms/[classroomId]/enrollment-requests/[requestId]/deny route.ts

0% Statements 0/69
0% Branches 0/1
0% Functions 0/1
0% Lines 0/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                                                                                           
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db, schema } from '@/db'
import { denyEnrollmentRequest, getLinkedParentIds, getTeacherClassroom } from '@/lib/classroom'
import { emitEnrollmentRequestDenied } from '@/lib/classroom/socket-emitter'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * POST /api/classrooms/[classroomId]/enrollment-requests/[requestId]/deny
 * Teacher denies enrollment request
 *
 * Returns: { request }
 */
export const POST = withAuth(async (_request, { params }) => {
  try {
    const { classroomId, requestId } = (await params) as { classroomId: string; requestId: string }
    const userId = await getUserId()

    // Verify user is the teacher of this classroom
    const classroom = await getTeacherClassroom(userId)
    if (!classroom || classroom.id !== classroomId) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const request = await denyEnrollmentRequest(requestId, userId, 'teacher')

    // Emit socket event for real-time updates
    try {
      // Get classroom and player info for socket event
      const [classroomInfo] = await db
        .select({ name: schema.classrooms.name })
        .from(schema.classrooms)
        .where(eq(schema.classrooms.id, classroomId))
        .limit(1)

      const [playerInfo] = await db
        .select({ name: schema.players.name })
        .from(schema.players)
        .where(eq(schema.players.id, request.playerId))
        .limit(1)

      if (classroomInfo && playerInfo) {
        // Get parent IDs to notify
        const parentIds = await getLinkedParentIds(request.playerId)

        await emitEnrollmentRequestDenied(
          {
            requestId,
            classroomId,
            classroomName: classroomInfo.name,
            playerId: request.playerId,
            playerName: playerInfo.name,
            deniedBy: 'teacher',
          },
          { userIds: parentIds }
        )
      }
    } catch (socketError) {
      console.error('[Teacher Deny] Failed to emit socket event:', socketError)
    }

    return NextResponse.json({ request })
  } catch (error) {
    console.error('Failed to deny enrollment request:', error)
    const message = error instanceof Error ? error.message : 'Failed to deny enrollment request'
    return NextResponse.json({ error: message }, { status: 500 })
  }
})